from msg import *
from msgpasser import *
from Reliabiltiy import handle_failure
import configfile

def send_message_to_neighbors (mp, msg) :

    #print "GET NEIGHBORS"
    #print configfile.get_node_neighbors(mp.thisName)

    listDest = []
    print configfile.get_node_neighbors(mp.thisName)
    for item in configfile.get_node_neighbors(mp.thisName) :
    #for item in mapS['group'] :
        if not item == mp.thisName :
            if not item == msg.src:
                listDest.append(item)

    msg.src = mp.thisName
    msg.dest = listDest
    msg.destArray= listDest

    #print "SEND message src %s Message type is : %s" % (msg.src, msg.mtype)
    #print " SEND msg.broadcastSource is : " + msg.broadcastSource
    #print "SEND Destinations are :",
    #print msg.dest
    failedSending =  mp.send(msg)
    #print "failedSending : ",
    #print failedSending
    if len(failedSending) != 0 :
        fail_msg = Message(mp.thisName, None, None, failedSending, 'node_failure', mp.thisName)
        handle_failure(mp, fail_msg)

    return failedSending
